package List;

public class DetectCycle142 {
    public ListNode detectCycle(ListNode head){
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                ListNode cur = head;
                while(cur != fast){
                    cur = cur.next;
                    fast = fast.next;
                }
                return fast;
            }
        }
        return null;
    }
}
